package com.cbl.v.ssczs.view.activity;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.util.ActivityGroup;
import com.cbl.v.ssczs.mode.util.NetStatusUtil;
import com.cbl.v.ssczs.view.agent.Html5WebView;

public class BallActivity extends BaseActivity {


    private LinearLayout mLayout, mNoNetLayout;
    private Html5WebView mWebView;
    private SwipeRefreshLayout mSwipeRefresh;
    private String webUrl;

    @Override
    protected int getLayout() {
        return R.layout.fragment_agentweb;
    }

    @Override
    public void initView() {
        hideTitleBar();
        mLayout = (LinearLayout) findViewById(R.id.web_layout);

        mNoNetLayout = (LinearLayout) findViewById(R.id.linearLayout_no_net);

        if (!NetStatusUtil.isConnected(this)) {
            mNoNetLayout.setVisibility(View.VISIBLE);
        } else {
            mNoNetLayout.setVisibility(View.GONE);
        }
        initWebView();
        initRefresh();
                initToolBar();
    }

    private void initWebView() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams
                .MATCH_PARENT);
        mWebView = new Html5WebView(getContext());
        mWebView.setLayoutParams(params);
        webUrl = "https://ic.snssdk.com/vertical/caipiao/report/";
        mWebView.loadUrl(webUrl);
        //mWebView.loadUrl("http://m.500.com/info/kaijiang/#h5");
        //mWebView.loadUrl("https://ic.snssdk.com/vertical/caipiao/report/");
        mLayout.addView(mWebView);

    }

    private void initRefresh() {
        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
        //设置刷新时动画的颜色，可以设置4个
        mSwipeRefresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!NetStatusUtil.isConnected(getContext())) {
                                    mNoNetLayout.setVisibility(View.VISIBLE);
                                    Toast.makeText(getContext(), "无网络", Toast.LENGTH_SHORT).show();
                                } else {
                                    mNoNetLayout.setVisibility(View.GONE);
                                    mWebView.reload();
                                }

                                mSwipeRefresh.setRefreshing(false);
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initToolBar() {
        Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
        TextView title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("重庆时时彩开奖走势");
        toolBar.setTitle("");
        toolBar.setTitleTextColor(Color.WHITE);
    }

    @Override
    public void initData() {

                mWebView.setOnTouchScreenListener(new Html5WebView.OnTouchScreenListener() {
                    @Override
                    public void onFinish() {
                        if (mWebView.canGoBack()) {
                        } else {
                        }
                    }

                    @Override
                    public void onStart() {
                    }
                });
    }

    @Override
    public void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    public void initListener() {

    }

    private long lastBackPress;

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            if (mWebView.getUrl().equals(webUrl)) {
                super.onBackPressed();
            } else {
                mWebView.goBack();
                mWebView.goForward();
            }
        } else {
            if (System.currentTimeMillis() - lastBackPress < 1000) {
                ActivityGroup.finishAllActivity();
                android.os.Process.killProcess(android.os.Process.myPid());
                super.onBackPressed();
            } else {
                lastBackPress = System.currentTimeMillis();
                Toast.makeText(BallActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
